package com.dean.practice.consumer.ctrl;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
 * @author Dean
 * @date 2020-11-24
 */
@Slf4j
@RestController
@RequestMapping("/uaa/res")
public class UaaCtrl {


    @ApiOperation("获取登录用户")
    @GetMapping(path = "/")
    public Object userInfo(@AuthenticationPrincipal Principal principal) {
        return principal;
    }

    @ApiOperation("获取JWT信息：仅当token类型为JWT")
    @GetMapping(path = "/jwt")
    public Object jwtInfo(@AuthenticationPrincipal Jwt jwt) {
        return jwt;
    }


    @ApiOperation("当前用户有该权限")
    @PreAuthorize("hasAuthority('ROLE_001')")
    @GetMapping("/api/hasRole")
    public ResponseEntity<String> hasRole() {
        return ResponseEntity.ok("Yes,Welcome");
    }

    @ApiOperation("当前用户无该权限：预期响应403")
    @PreAuthorize("hasAuthority('ROLE_005')")
    @GetMapping("/api/noRole")
    public ResponseEntity<String> noRole() {
        return ResponseEntity.ok("No permisson");
    }
}